package com.itheima.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 在保证服务器端没有问题的情况下再去写前端的代码
 */
@WebServlet("/Demo1CheckUserServlet")
public class Demo1CheckUserServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置服务器响应类型和编码，注：这里使用纯文本的格式 如：<h1>等网页标签不起作用的
        response.setContentType("text/plain;charset=utf-8");
        PrintWriter out = response.getWriter();
        //1.获取客户端提交的用户名
        String user = request.getParameter("user");
        //2.调用业务层判断用户名是否存在
        if ("NewBoy".equalsIgnoreCase(user)) {
            out.print("用户名已经存在");
        }
        else {
            out.print("可以注册");
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
